const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
class MyPromise {
    constructor(executor) {
        try {
            executor(this.resolve, this.reject)
        } catch (e) {
            this.reject(e)
        }
    }
    //promise状态
    status = PENDING
    //成功后的值
    value = undefined
    //失败后的值
    reason = undefined
    //成功回调
    successcallback = []
    //失败回调
    failcallback = []
    resolve = value => {
        //如果程序不是pending状态，不继续执行
        if (this.status !== PENDING) return
        this.status = FULFILLED
        this.value = value
        //如果存在成功回调 调用
        // this.successcallback && this.successcallback(this.value)
        while (this.successcallback.length) {
            this.successcallback.shift()()
        }
    }
    reject = reason => {
        if (this.status !== PENDING) return
        this.status = REJECTED
        this.reason = reason
        // this.failcallback && this.failcallback(this.reason)
        while (this.failcallback.length) this.failcallback.shift()()
    }
    then(successcallback, failcallback) {
        //控制then方法中传递的参数可选
        successcallback = successcallback ? successcallback : value => value;
        failcallback = failcallback ? failcallback : reason => {throw reason}
        let promise2 = new MyPromise((resolve,reject) => {
            if (this.status === FULFILLED) {
                setTimeout(() => {
                    try {
                        let x = successcallback(this.value)
                        //判断x是普通值还是promise对象，如果是promise根据返回结果来决定调用resolve还是reject
                        resolvePromise(promise2, x, resolve, reject)
                    } catch (error) {
                        reject(error)
                    }
                }, 0)
            } else if (this.status === REJECTED) {
                setTimeout(() => {
                    try {
                        let x = failcallback(this.reason)
                        //判断x是普通值还是promise对象，如果是promise根据返回结果来决定调用resolve还是reject
                        resolvePromise(promise2, x, resolve, reject)
                    } catch (error) {
                        reject(error)
                    }
                }, 0)
            } else {
                //等待状态
                //将回调存储起来
                this.successcallback.push(()=>{
                    setTimeout(() => {
                        try {
                            let x = successcallback(this.value)
                            //判断x是普通值还是promise对象，如果是promise根据返回结果来决定调用resolve还是reject
                            resolvePromise(promise2, x, resolve, reject)
                        } catch (error) {
                            reject(error)
                        }
                    }, 0)
                })
                this.failcallback.push(()=>{
                    setTimeout(() => {
                        try {
                            let x = failcallback(this.reason)
                            //判断x是普通值还是promise对象，如果是promise根据返回结果来决定调用resolve还是reject
                            resolvePromise(promise2, x, resolve, reject)
                        } catch (error) {
                            reject(error)
                        }
                    }, 0)
                })
            }
        })
        return promise2
    }
    finally (callback) {
        return this.then(value => {
          return MyPromise.resolve(callback()).then(() => value);
        }, reason => {
          return MyPromise.resolve(callback()).then(() => { throw reason })
        })
      }
    catch (failCallback) {
        return this.then(undefined, failCallback)
      }
    static all (array) {
        let result = []
        let index = 0
        return new MyPromise((resolve,reject)=>{
            function addData(key,value){
                result[key] = value
                index++;
                if(index === result.length) {
                    resolve(result)
                }
            }
            for(let i =0;i<array.length;i++){
                let current = array[i]
                if(current instanceof MyPromise){
                    current.then(value=>addData(i,value),reason=>reject(reason))
                }else{
                    addData(i,array[i])
                }
            }
        })
    }
    static resolve (value) {
        if(value instanceof MyPromise) return value
        return new MyPromise(resolve=>resolve(value))
    }
}
function resolvePromise(promise2,x,resolve,reject) {
    if(promise2 === x){
        return reject(new TypeError('出错了'))
    }
    if(x instanceof MyPromise){
        // x.then(res=>resolve(res),reason=>reject(reason))
        x.then(resolve,reject)
    }else{
        resolve(x)
    }
}
module.exports = MyPromise